package com.lsh.controller;

import com.lsh.entity.Result;
import com.lsh.pojo.User;
import com.lsh.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Enumeration;

@RestController
public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping("/user/login")
    public Result login(@RequestBody User user,HttpSession session){
            try {
                User login = userService.login(user);
                session.setAttribute("user",login);
                return new Result(true,"登录成功",login);
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println(e.getMessage());
                return new Result(false,e.getMessage());
            }
    }
    @RequestMapping("/user/logout")
    public Result logout(HttpSession session){
        session.invalidate();
        return new Result(true,"退出成功");
    }
    @RequestMapping("/set")
    public String setSession(HttpSession session){
        session.setAttribute("user","112233");
        return "设置成功";
    }
    @RequestMapping("/get")
    public String getSession(HttpSession session){
        System.out.println(session.getAttribute("user"));
        return "666";
    }
}
